<template>
  <div class="login">
    <Topbar gobackEnabled="true" :title="isAccountLogin ? '密码登录' : '登录'"/>

    <form v-if="isAccountLogin">
      <section class="input-container">
        <input type="text" placeholder="账号" v-model.lazy="account">
      </section>

      <section class="input-container">
        <input :type="isShowPassword ? 'type' : 'password'" placeholder="密码" v-model="password">
        <div class="button-switch" :class="{actived: isShowPassword}">
          <div class="circle-button" @click="isShowPassword = !isShowPassword"></div>
          <span>abc</span>
          <span>...</span>
        </div>
      </section>

      <section class="input-container vcode-container">
        <input type="text" placeholder="验证码" maxlength="4" v-model="vcode">
        <div class="vcode-img">
          <img v-if="vcodeImg" :src="vcodeImg">
          <div class="refresh-button" @click="refershVcodeImg">
            <p>看不清</p>
            <p>换一张</p>
          </div>
        </div>
      </section>
    </form>
    <form v-else>
      <p>其他方式暂不支持</p>
    </form>

    <p class="tips">温馨提示：未注册过的账号，登录时将自动注册</p>
    <p class="tips">注册过的用户可凭账号密码登录</p>

    <div class="submit-button" @click="doLogin">登录</div>
    <router-link to="/forget" class="forget-password" v-if="isAccountLogin">重置密码？</router-link>

    <Alert v-if="alertOption.visible" :option="alertOption"/>
  </div>
</template>

<script>
import Topbar from '../../components/Topbar';
import Alert from '../../components/Alert';
import { getVcodeImg, accountLogin } from '../../etc/datas';
import { mapActions } from 'vuex';
export default {
  data() {
    return {
      isAccountLogin: true, // 使用账号密码登录
      isShowPassword: false, // 是否显示密码

      account: '', // 用户名
      password: '', // 密码
      vcode: '', // 输入的验证码
      vcodeImg: '', // 验证码图片

      alertOption: {
        visible: false,
        close() {
          this.visible = false;
        }
      },

      end: null
    };
  },
  beforeMount() {
    this.refershVcodeImg();
  },
  computed: {},
  methods: {
    ...mapActions(['saveUserInfo']),
    refershVcodeImg() {
      this.vcode = '';
      getVcodeImg(data => {
        this.vcodeImg = data.code;
      });
    },
    async doLogin() {
      if (!this.isAccountLogin) {
        return this.alert('暂时不支持此登录方式');
      }

      if (!this.account) {
        return this.alert('请输入手机号/邮箱/用户名');
      }
      if (!this.password) {
        return this.alert('请输入密码');
      }

      if (!this.vcode) {
        return this.alert('请输入验证码');
      }

      let data = await accountLogin(
        null,
        this.account,
        this.password,
        this.vcode
      );

      if (data.user_id) {
        await this.saveUserInfo(data);
        this.$router.back();
      } else {
        this.alert(data.message);
        this.refershVcodeImg();
      }
    },
    alert(message) {
      this.alertOption.visible = true;
      this.alertOption.message = message;
    }
  },
  components: {
    Topbar,
    Alert
  }
};
</script>

<style lang="scss">
@import '../../styles/mixin';
.login {
  p,
  span,
  input {
    font-family: Helvetica Neue, Tahoma, Arial;
  }

  form {
    background-color: #fff;
    margin-top: 0.6rem;

    .input-container {
      display: flex;
      justify-content: space-between;
      padding: 0.6rem 0.8rem;
      border-bottom: 1px solid #f1f1f1;
      input {
        @include sc(0.7rem, #666);
      }
      button {
        @include sc(0.65rem, #fff);
        font-family: Helvetica Neue, Tahoma, Arial;
        padding: 0.28rem 0.4rem;
        border: 1px;
        border-radius: 0.15rem;
      }
      .right_phone_number {
        background-color: #4cd964;
      }
    }

    .button-switch {
      background-color: #ccc;
      display: flex;
      justify-content: center;
      @include wh(2rem, 0.7rem);
      padding: 0 0.2rem;
      border: 1px;
      border-radius: 0.5rem;
      position: relative;
      .circle-button {
        transition: all 0.3s;
        position: absolute;
        top: -0.2rem;
        z-index: 1;
        left: -0.3rem;
        @include wh(1.2rem, 1.2rem);
        box-shadow: 0 0.026667rem 0.053333rem 0 rgba(0, 0, 0, 0.1);
        background-color: #f1f1f1;
        border-radius: 50%;
      }

      span {
        @include sc(0.45rem, #fff);
        transform: translateY(0.05rem);
        line-height: 0.6rem;
      }
      span:nth-of-type(2) {
        transform: translateY(-0.08rem);
      }
    }
    .button-switch.actived {
      background-color: #4cd964;
      .circle-button {
        transform: translateX(1.3rem);
      }
    }

    .vcode-container {
      height: 2.2rem;
      .vcode-img {
        display: flex;
        align-items: center;
        img {
          @include wh(3.5rem, 1.5rem);
          margin-right: 0.2rem;
        }
        .refresh-button {
          display: flex;
          flex-direction: 'column';
          flex-wrap: wrap;
          width: 2rem;
          justify-content: center;
          p {
            @include sc(0.55rem, #666);
          }
          p:nth-of-type(2) {
            color: #3b95e9;
            margin-top: 0.2rem;
          }
        }
      }
    }
  }

  .tips {
    @include sc(0.5rem, red);
    padding: 0.4rem 0.6rem;
    line-height: 0.5rem;
    a {
      color: #3b95e9;
    }
  }

  .submit-button {
    display: block;
    margin: 0 0.5rem 1rem;
    @include sc(0.7rem, #fff);
    background-color: #4cd964;
    padding: 0.5rem 0;
    border: 1px;
    border-radius: 0.15rem;
    text-align: center;
  }

  .forget-password {
    float: right;
    @include sc(0.6rem, #3b95e9);
    margin-right: 0.3rem;
  }
}
</style>